package cn.stylefeng.guns.modular.kehao.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.model.params.BasicPutgoodsParam;
import cn.stylefeng.guns.modular.kehao.entity.KhLyOutOrder;
import cn.stylefeng.guns.modular.kehao.model.params.KhLyOutOrderParam;
import cn.stylefeng.guns.modular.kehao.service.KhLyOutOrderService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


/**
 * 可好出库订单控制器
 *
 * @author zzh
 * @Date 2025-09-30 10:13:18
 */
@Controller
@RequestMapping("/khLyOutOrder")
public class KhLyOutOrderController extends BaseController {

    private String PREFIX = "/kehao/khLyOutOrder";

    @Autowired
    private KhLyOutOrderService khLyOutOrderService;

    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     *
     * @author zzh
     * @Date 2025-09-30
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/khLyOutOrder.html";
    }

    /**
     * 新增页面
     *
     * @author zzh
     * @Date 2025-09-30
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/khLyOutOrder_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zzh
     * @Date 2025-09-30
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/khLyOutOrder_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zzh
     * @Date 2025-09-30
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(KhLyOutOrderParam khLyOutOrderParam) {
        this.khLyOutOrderService.add(khLyOutOrderParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zzh
     * @Date 2025-09-30
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(KhLyOutOrderParam khLyOutOrderParam) {
        this.khLyOutOrderService.update(khLyOutOrderParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zzh
     * @Date 2025-09-30
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(KhLyOutOrderParam khLyOutOrderParam) {
        this.khLyOutOrderService.delete(khLyOutOrderParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zzh
     * @Date 2025-09-30
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(KhLyOutOrderParam khLyOutOrderParam) {
        KhLyOutOrder detail = this.khLyOutOrderService.getById(khLyOutOrderParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zzh
     * @Date 2025-09-30
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(KhLyOutOrderParam khLyOutOrderParam,String condition ) {
        if(ToolUtil.isEmpty(khLyOutOrderParam.getStartTime())){
            khLyOutOrderParam.setStartTime(DateUtil.getDate());
        }
        if(ToolUtil.isEmpty(khLyOutOrderParam.getEndTime())){
            khLyOutOrderParam.setEndTime(DateUtil.getDate());
        }
        return this.khLyOutOrderService.findPageBySpec(khLyOutOrderParam,condition);
    }

    /**
     * 跳转到导入页面
     */
    @RequestMapping("/toimportExcel")
    public String toimportExcel() {
        return PREFIX + "/import.html";
    }

    /**
     * 导入excel
     */
    @RequestMapping(value = "/importExcel" , method = {RequestMethod.POST} )
    @ResponseBody
    public ResponseData importExcel(@RequestPart("file") MultipartFile file, BasicPutgoodsParam basicPutgoodsParam)  {
        ResponseData data = new ResponseData();
        //
        try {
            data = khLyOutOrderService.getExceldata(file,userService.getUser());//处理导入数据
        } catch (Exception e) {
            data.setSuccess(false);
            e.printStackTrace();
        }
        return data;
    }

    /**
     * 下载模板
     * @throws ServletException , IOException
     */
    @RequestMapping("downloadExcel")
    public void downloadExcel(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
        String path = "assets/excel/kehaockdd.xls";
        try {
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
            OutputStream outputStream = response.getOutputStream();
            //设置下载的文件类型
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //设置下载文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(("可好出库订单" + ".xls").getBytes(), "iso-8859-1"));
            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
            inputStream.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 计算运费
     * @param ids
     * @return
     */
    @RequestMapping("/btnSubmit")
    @ResponseBody
    public ResponseData btnSubmit(@RequestParam("ids") String ids) {
        return this.khLyOutOrderService.btnSubmit(ids,userService.getUser());
    }

}


